package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.enums.BusinessIdentifyingEnum;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyMountainPeak;
import com.zl.tby.dto.TbyMountainPeakQuery;
import com.zl.tby.repository.TbyMountainPeakRepo;
import com.zl.tby.service.ITbyFileBusinessService;
import com.zl.tby.service.ITbyMountainPeakService;
import com.zl.tby.vo.TbyMountainPeakForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 山峰信息接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyMountainPeakServiceImpl implements ITbyMountainPeakService {

    @Db
    private TbyMountainPeakRepo tbyMountainPeakRepo;
    @Inject
    private ITbyFileBusinessService tbyFileBusinessService;

    @Override
    @Tran
    public R<TbyMountainPeak> add(TbyMountainPeakForm form) {
        TbyMountainPeak entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyMountainPeakRepo.insert(entity, true);
        entity.setId(id);
        this.tbyFileBusinessService.saveAttach(BusinessIdentifyingEnum.MOUNTAIN_PEAK.toString(), id, form.getAttach());
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyMountainPeak> update(TbyMountainPeakForm form) {
        TbyMountainPeak entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyMountainPeakRepo.updateById(entity, false);
        this.tbyFileBusinessService.saveAttach(BusinessIdentifyingEnum.MOUNTAIN_PEAK.toString(), entity.getId(), form.getAttach());
        return R.ok(entity);
    }

    @Override
    public int delete(Long id) {
        return tbyMountainPeakRepo.deleteById(id);
    }

    @Override
    public TbyMountainPeak get(Long id) {
        return tbyMountainPeakRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyMountainPeak> page(TbyMountainPeakQuery query) {
        IPage<TbyMountainPeak> iPage = tbyMountainPeakRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getPosition()), "position like ?", "%" + query.getPosition() + "%");
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyMountainPeak> list(TbyMountainPeakQuery query) {
        return tbyMountainPeakRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue().andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getPosition()), "position like ?", "%" + query.getPosition() + "%");
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
    }
}
